# -*- coding:utf-8 -*-

import requests
import re

proxies = {
    "https": "http://135.245.48.34:8000",
    "http": "http://135.245.48.34:8000",
  }

#网络爬虫之丑事百科


class csbk():
    def __init__(self):
        #网址：
        self.url ='https://www.qiushibaike.com/hot/page/'
        #网页号码
        self.Index = 1
        #模拟浏览器
        self.user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) ' \
                          'AppleWebKit/537.36 (KHTML, like Gecko) ' \
                          'Chrome/63.0.3239.132 Safari/537.36'
        # 初始化headers
        self.headers = {'User-Agent': self.user_agent}
        #提前加载开关
        self.enable = True
        #存储信息内容
        self.storiesLst = []


    #获取网页内容
    def getpage(self):
        url = self.url + str(self.Index) + "/"
        try:
            resp = requests.get(url, headers=self.headers, proxies=proxies, timeout=5)
            if resp.status_code == 200:
                self.Index += 1
                return resp.content
        except Exception,e:
            if hasattr(e,'reason'):
                print u'网页链接错误原因：\n',e.reason

    #从网页中筛选出想要的内容
    def getpageItems(self):
        page_content = self.getpage()
        if not page_content:
            print u'网页加载失败！'

        #丑事百科-不含图片
        regx = re.compile(r'<div class="author clearfix">.*?alt="(.*?)">.*?<div class="content">\n<span>(.*?)</span>',
                          re.S)
        Items = re.findall(regx, page_content)
        return Items

    #判断是否需要获取新的故事
    def newstories (self):
        if self.enable is True and len(self.storiesLst) < 1:
            print "正在获取中......"
            self.storiesLst.extend(self.getpageItems())
            return True
        return None

    #开始，判断输入并打印结果
    def start(self):
        self.storiesLst.extend(self.getpageItems())
        while True:
            input = raw_input(u"输入回车键看浏览笑话，退出按'Q':")
            if input == "Q":
                self.enable = False
                break
            elif len(self.storiesLst) >= 1:
                print len(self.storiesLst)
                tmp = self.storiesLst[0][1]
                self.storiesLst.pop(0)
                print tmp.strip().replace("<br/>",'\n') + '\n'
                self.newstories()
            else:
                print u"休息下再来看吧~"
                break
        return u"祝你快乐每一天!"

if __name__ == "__main__":
    first = csbk()
    first.start()

